package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FoodDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Food;
import com.itheima.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = FoodService.class)
@Transactional
public class FoodServiceImpl implements FoodService {
    @Autowired
    private FoodDao foodDao;

    @Override//分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        PageHelper.startPage(currentPage, pageSize);
        String queryString = queryPageBean.getQueryString();
        Page<Food> page = foodDao.findByCondition(queryString);
        List<Food> result = page.getResult();
        for (Food food : result) {
            Integer typeId = food.getTypeId();
            String type = foodDao.getFoodType(typeId);
            food.setType(type);
        }
        return new PageResult(page.getTotal(),result);
    }

    @Override//批量添加
    public void addList(List<Food> foodList) {
        for (Food food : foodList) {
            foodDao.add(food);
        }
    }

    @Override //单项添加
    public void add(Food food) {
        foodDao.add(food);
    }

    @Override //删除
    public void delete(Integer id) {
        foodDao.delete(id);
    }

    @Override //修改
    public void edit(Food food) {
        foodDao.edit(food);
    }

    @Override //查询食物种类
    public List<Map> getFoodTypes() {
        List<Map> foodTypes = foodDao.getFoodTypes();
        System.out.println(foodTypes);
        return foodTypes;
    }

    @Override //按id查询
    public Food findById(Integer id) {
        return foodDao.findById(id);
    }

    @Override
    public List<Food> findAll() {
        return foodDao.findAll();
    }
}
